package org.example.service.impl;

import org.example.constant.OcrItem;
import org.example.handle.Handle;
import org.example.handle.baidu.BaiduOcrHandle;
import org.example.handle.tess.TessOcrHandle;
import org.example.service.OcrService;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

public class OcrServiceImpl implements OcrService {

    private Map<OcrItem, Handle> handleMap = new HashMap<OcrItem, Handle>() {{
        put(OcrItem.TESS, new TessOcrHandle());
        put(OcrItem.BAIDU, new BaiduOcrHandle());
    }};

    @Override
    public Object general(OcrItem item, BufferedImage image) {
        Handle handle = handleMap.get(item);
        if (null != handle) {
            return handle.general(image);
        }
        for (Handle value : handleMap.values()) {
            Object general = value.general(image);
            // TODO 如果出现错误或者还是不存在结果继续执行，否则终止
            if (null != general) {
                return general;
            }
        }
        throw new RuntimeException("暂未识别出结果");
    }
}
